import http from "./http.js";
import api from "./api.js";
let serverData = {
	//注册
	wechatLogin: function(this_vue, callback = function() {}) {
        // console.log('wechatLogin')
		let _self = this_vue;
		uni.login({
			provider: 'weixin',
			success: function(loginRes) {
				let option = {
					url: api.wx_login,
					method: 'POST',
					data: {
						code: loginRes.code,
						appId: 'wx5d37f17d9cd8c819',
					}
				};
				http.request(option, function(res) {
						let result = res.data.data;
						callback()
						store.commit('login', result)
					},
					function(res) {
						// console.log(res)
					}
				);

			}
		});
	},


	//解密手机号
	wx_getPhoneNumber: function(this_vue, detail, callback = function() {}) {
		let _self = this_vue;
		uni.login({
			provider: 'weixin',
			success: function(loginRes) {
				let option = {
					url: api.wx_getPhoneNumber,
					method: 'POST',
					data: {
						code: loginRes.code,
						encryptedData: detail.encryptedData,
						iv: detail.iv,
						openId: store.state.userInfo.openId,
					}
				};
				http.request(
					option,
					function(res) {
						let result = res.data.data;
						let userInfo = store.state.temp.userInfo;
						userInfo.phone = res.data.data.phoneNumber;
						userInfo.openId = store.state.userInfo.openId;
						userInfo.unionId = store.state.userInfo.unionId;
						userInfo.controlId = store.state.temp.controlId;
						commonData.wx_register(this_vue, userInfo);
						callback(res)
					},
					function(res) {
					}
				);


			}
		});
	},
	//注册信息
	wx_register: function(this_vue, userInfo) {
		let _self = this_vue;
		uni.login({
			provider: 'weixin',
			success: function(loginRes) {
				let option = {
					url: api.wx_register,
					method: 'POST',
					data: userInfo
				};
				http.request(option, function(res) {
						let result = res.data.data;
						store.commit('login', result);
					},
					function(res) {
					}
				);

			}
		});
	},
	//获取测试
	wx_test: function(this_vue, data, callback) {
		let _self = this_vue;
		let option = {
			url: api.app_test,
			method: 'GET',
			data: data
		};
		http.request(
			option,
			function(res) {
				callback(res)
			},
			function(res) {
				 console.log("失败")
			}
		);
	},
	//获取通告列表
	app_notice_list: function(this_vue, data, callback) {
		let _self = this_vue;
		let option = {
			url: api.app_notice_list,
			method: 'GET',
			data: data
		};
		http.request(
			option,
			function(res) {
				callback(res)
			},
			function(res) {
				 console.log("失败")
			}
		);
	},
	//获取事件列表
	app_events_list: function(this_vue, data, callback) {
		let _self = this_vue;
		let option = {
			url: api.app_events_list,
			method: 'GET',
			data: data
		};
		http.request(
			option,
			function(res) {
				callback(res)
			},
			function(res) {
				 console.log("失败")
			}
		);
	},
	//获取轮播图
	app_swiper_list: function(this_vue, data, callback) {
		let _self = this_vue;
		let option = {
			url: api.app_swiper_list,
			method: 'GET',
			data: data
		};
		http.request(
			option,
			function(res) {
				callback(res)
			},
			function(res) {
				 console.log("失败")
			}
		);
	},
	//APP登录
	app_login: function(this_vue, data, callback) {
		let _self = this_vue;
		let option = {
			url: api.app_login,
			method: 'POST',
			data: data
		};
		http.request(
			option,
			function(res) {
				callback(res)
			},
			function(res) {
				 console.log("失败")
			}
		);
	},
	//APP注册
	app_register: function(this_vue, data, callback) {
		let _self = this_vue;
		let option = {
			url: api.app_register,
			method: 'POST',
			data: data
		};
		http.request(
			option,
			function(res) {
				callback(res)
			},
			function(res) {
				 console.log("失败")
			}
		);
	}
}

module.exports = serverData;
